package com.controller;

import com.bean.Goods;
import com.biz.GoodsBizService;
import com.dto.BaseMsg;


import com.dto.goods.GoodsBaseInfoDTO;
import com.dto.goods.GoodsDeleteDTO;
import com.dto.goods.GoodsUpdateDTO;

import com.exception.BizException;
import com.exception.DeleteException;
import com.exception.InsertException;
import com.exception.UpdateException;
import com.myenum.BizEnumString;
import com.myenum.CodeEnum;
import com.utils.JwtUtil;
import com.utils.PageUtil;
import com.utils.checkutil.GoodsCheckUtil;
import com.utils.checkutil.UserCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品的后台管理：
 * 商品的增删改查
 * 发布商品秒杀活动v2.0会迭代
 * @author FatterShadystart
 * @create 2023-02-19 21:19
 */
@RestController
@RequestMapping("/goodsManage")
@Slf4j
public class GoodsManageController {


    @Resource
   private GoodsBizService goodsBizService;


    /**
     * 分页查询所有商品,因为axios不支持json 所以用postMapping
     */
    @ResponseBody
    @PostMapping("/queryAllGoods")
    @RequiresRoles(value = {"admin"})
    public BaseMsg queryGoods(@RequestBody GoodsBaseInfoDTO goodsBaseInfoDTO) {
        //参数校验
        if (!GoodsCheckUtil.checkGoodsQuery(goodsBaseInfoDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        //查询商品
        Map<String, Object> goodsQueryMap = goodsBizService.queryGoodsAllByPage(goodsBaseInfoDTO);
        List<Goods> goodsList = (List<Goods>) goodsQueryMap.get(BizEnumString.GOODS_LIST.getMsg());
        Integer allGoodsCount = (Integer) goodsQueryMap.get(BizEnumString.ALL_GOODS_COUNT.getMsg());
        log.info("goodsQueryMap:{}", goodsQueryMap);
        log.info("goodsList:{}", goodsList);
        log.info("allGoodsCount:{}", allGoodsCount);

        //构建分页
        Map<String, Object> pageStartAndEnd = PageUtil.getPageParam(goodsBaseInfoDTO.getBaseInfoDTO(), allGoodsCount, goodsList);
        log.info("pageStartAndEnd:{}", pageStartAndEnd);
        return BaseMsg.succeess().succeessBuildData(pageStartAndEnd);
    }

    /**
     * 分页模糊查询 根据商品名称、商品描述查询商品
     *
     * @param goodsBaseInfoDTO
     * @return
     */
    @ResponseBody
    @PostMapping("/fuzzyQuery")
    @RequiresRoles(value = {"admin"})
    public BaseMsg queryGoodsByCondition(@RequestBody GoodsBaseInfoDTO goodsBaseInfoDTO) {
        //参数校验
        if (!GoodsCheckUtil.checkGoodsQueryByCondition(goodsBaseInfoDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        //查询商品
        Map<String, Object> goodsQueryMap = goodsBizService.queryGoodsByCondition(goodsBaseInfoDTO);
        List<Goods> goodsRoleList = (List<Goods>) goodsQueryMap.get(BizEnumString.GOODS_LIST_QUERY.getMsg());
        Integer allGoodsCount = (Integer) goodsQueryMap.get(BizEnumString.ALL_GOODS_COUNT.getMsg());
        log.info("goodsQueryMap:{}", goodsQueryMap);
        log.info("goodsRoleList:{}", goodsRoleList);
        log.info("allGoodsCount:{}", allGoodsCount);
        //构建分页
        Map<String, Object> pageStartAndEnd = PageUtil.getPageParam(goodsBaseInfoDTO.getBaseInfoDTO(), allGoodsCount, goodsRoleList);
        log.info("pageStartAndEnd:{}", pageStartAndEnd);
        return BaseMsg.succeess().succeessBuildData(pageStartAndEnd);
    }


    /**
     * 插入商品，传入的goodsPrice单位为分
     * @param goodsBaseInfoDTO
     * @return
     */
    @ResponseBody
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public BaseMsg insertGoods(@RequestBody GoodsBaseInfoDTO goodsBaseInfoDTO, @RequestHeader("Access-Token") String jwt) throws InsertException {
        Long adminId = Long.valueOf(JwtUtil.getClaimUserId(jwt, BizEnumString.USER_ID.getMsg()));
        goodsBaseInfoDTO.getGoods().setCreatorId(adminId);
        goodsBaseInfoDTO.getGoods().setUpdatorId(adminId);
        //参数校验
        if (!GoodsCheckUtil.checkGoodsInsert(goodsBaseInfoDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        log.info("goodsBaseInfoDTO:{}", goodsBaseInfoDTO);
        //注册
        goodsBizService.insertGoods(goodsBaseInfoDTO);
        return BaseMsg.succeess();
    }



    /**
     * 删除商品-单个和批量公用
     * @param  goodsDeleteDTO
     * @return
     */
    @ResponseBody
    @DeleteMapping("/deleteBatch")
    @RequiresRoles(value = {"admin"})
    public BaseMsg deleteGoodsBatch(@RequestBody GoodsDeleteDTO goodsDeleteDTO,@RequestHeader("Access-Token") String jwt) throws DeleteException {
        Long updatorId = Long.valueOf(JwtUtil.getClaimUserId(jwt,BizEnumString.USER_ID.getMsg()));
        List<Long> goodsIdList = goodsDeleteDTO.getGoodsIdList();
        if (CollectionUtils.isEmpty(goodsIdList)||!GoodsCheckUtil.checkUpdatorId(updatorId)){
            throw new DeleteException(CodeEnum.PARAM_CHECK_FAILED.getMsg());
        }
        log.info("goodsDeleteDTO:{}", goodsDeleteDTO);
        log.info("updatorId:{}", updatorId);
        //删除
        goodsBizService.deleteBatchGoods(goodsIdList,updatorId);
        return BaseMsg.succeess();
    }


    /**
     * 修改商品
     * @param goodsUpdateDTO
     * @param jwt
     * @return
     * @throws UpdateException
     */
    @ResponseBody
    @PutMapping
    @RequiresRoles(value={"admin"})
    public BaseMsg updateGoods(@RequestBody GoodsUpdateDTO goodsUpdateDTO, @RequestHeader("Access-Token") String jwt) throws UpdateException {
        Long updatorId = Long.valueOf(JwtUtil.getClaimUserId(jwt,BizEnumString.USER_ID.getMsg()));
        //参数校验
        if (!GoodsCheckUtil.checkUpdatorId(updatorId) || !GoodsCheckUtil.checkGoodsUpdate(goodsUpdateDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        log.info("goodsUpdateDTO:{}", goodsUpdateDTO);
        log.info("updatorId:{}", updatorId);
        //修改
        goodsBizService.updateGoods(goodsUpdateDTO,updatorId);
        return BaseMsg.succeess();
    }



}
